package srv

import (
	"back/entity/dto"
	"back/entity/nailEntity"
	"back/entity/nailRepo"
	requset "back/router/request"
	"context"
	"fmt"
	"golang.org/x/oauth2"
	"utilx/logx"
	"utilx/middleware"
)

func (c NailMember) Login(rec requset.LoginReq) (string, error) {
	userInfo, err := c.Repo.NailMember.Where(nailRepo.NailMember.Name.Eq(rec.UserName), nailRepo.NailMember.Password.Eq(rec.Password)).First()
	if err != nil {
		return "", err
	}
	token, err := middleware.CreatJwtTokenWithClaims[nailEntity.NailMember](middleware.UserClaims[nailEntity.NailMember]{
		Data: *userInfo,
	})
	return token, err
}

func (c NailMember) Oauth2Token(rec dto.Oauth2) (*oauth2.Token, error) {
	ctx := context.Background()
	verifier := oauth2.GenerateVerifier()
	conf := rec.ToOauth2()
	url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
	fmt.Printf("Visit the URL for the auth dialog: %v", url)
	tok, err := conf.Exchange(ctx, rec.Code, oauth2.VerifierOption(verifier))
	if err != nil {
		logx.Debug(err.Error())
	}
	oauth2.GenerateVerifier()
	return tok, nil
}
func (c NailMember) Oauth2Code(rec dto.Oauth2) (string, error) {
	return oauth2.GenerateVerifier(), nil

}
func (c NailMember) Oauth2RefreshToken(rec dto.Oauth2) (string, error) {
	toOauth2 := rec.ToOauth2()
	toOauth2.Exchange(context.Background(), "")
	return "asd123ads", nil
}
